package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {

    /**
     * 插入订单
     * @param orders
     */
    @Insert("insert into orders (number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, amount, remark, phone, address, user_name, consignee, cancel_reason, rejection_reason, cancel_time, estimated_delivery_time, delivery_status, delivery_time, pack_amount, tableware_number, tableware_status) VALUES " +
            "(#{number},#{status},#{userId},#{addressBookId},#{orderTime},#{checkoutTime},#{payMethod},#{payStatus},#{amount},#{remark},#{phone},#{address},#{userName},#{consignee},#{cancelReason},#{rejectionReason},#{cancelTime},#{estimatedDeliveryTime},#{deliveryStatus},#{deliveryTime},#{packAmount},#{tablewareNumber},#{tablewareStatus})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    void insert(Orders orders);

    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 历史订单查询
     *
     * @return
     */
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据订单ID查询订单
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders getByOrderId(Integer id);

    /**
     * 取消订单
     * @param id
     */
    @Delete("delete  from orders where id = #{id}")
    void delById(Integer id);

    /**
     * 查询待派送数量
     * @return
     */
    @Select("select count(*) from orders where status = 3")
    Integer countConfirmed();

    /**
     * 查询派送中数量
     * @return
     */
    @Select("select count(*) from orders where status = 4")
    Integer countDeliveryInProgress();

    /**
     * 查询待接单数量
     * @return
     */
    @Select("select count(*) from orders where status = 2")
    Integer countToBeConfirmed();

    /**
     * 接单
     * @param ordersConfirmDTO
     */
    @Update("update orders set status = 3 where id = #{id}")
    void confirm(OrdersConfirmDTO ordersConfirmDTO);

    /**
     * 拒单
     * @param ordersRejectionDTO
     */
    @Update("update orders set status = 6,rejection_reason = #{rejectionReason} where id = #{id}")
    void rejection(OrdersRejectionDTO ordersRejectionDTO);

    /**
     * 管理端取消订单
     * @param ordersCancelDTO
     */
    @Update("update orders set status = 6,cancel_reason = #{cancelReason} where id = #{id}")
    void cancelAdmin(OrdersCancelDTO ordersCancelDTO);

    /**
     * 管理端派送订单
     * @param id
     */
    @Update("update orders set status = 4 where id = #{id}")
    void delivery(Integer id);

    /**
     * 管理端完成订单
     * @param id
     */
    @Update("update orders set status = 5 where id = #{id}")
    void complete(Integer id);

    /**
     * 根据状态和时间查询orders
     *
     * @param pendingPayment
     * @param time
     * @return
     */
    @Select("select * from orders where status  =#{pendingPayment} and order_time < #{time}")
    List<Orders> getByStatusAndOrderTimeLT(Integer pendingPayment, LocalDateTime time);


    @Update("update orders set status = #{orderStatus},pay_status = #{orderPaidStatus} ,checkout_time = #{check_out_time} where id = #{id}")
    void updateStatus(Integer orderStatus, Integer orderPaidStatus, LocalDateTime check_out_time, Long id);



    /**
     * 根据动态条件统计营业额数据
     * @param map
     * @return
     */
    Double sumByMap(Map map);

    /**
     * 根据动态条件统计订单数量
     * @param map
     * @return
     */
    Integer countByMap(Map map);
}
